package main

import (
	"encoding/json"
	"fmt"
	"github.com/spf13/cobra"
	"io"
	"net/http"
)

func addQueryCommands() {
	queryCmd := &cobra.Command{
		Use:   "query",
		Short: "数据查询操作",
	}

	// 设备详细信息
	deviceDataCmd := &cobra.Command{
		Use:   "device <name>",
		Short: "查询设备详细信息",
		Args:  cobra.MinimumNArgs(1),
		Run: func(cmd *cobra.Command, args []string) {
			name := args[0]
			url := fmt.Sprintf("http://%s/device/show/%s", ConsoleAddr(), name)
			req, err := http.NewRequest("GET", url, http.NoBody)
			if err != nil {
				fmt.Printf("创建请求失败: %w", err)
				return
			}

			req.Header.Set("Content-Type", "application/json")

			client := &http.Client{}
			resp, err := client.Do(req)
			if err != nil {
				fmt.Printf("请求发送失败: %w", err)
				return
			}
			defer resp.Body.Close()

			// 检查响应状态码
			if resp.StatusCode != http.StatusOK {
				fmt.Printf("服务器返回错误状态码: %d", resp.StatusCode)
				return
			}

			responseBody, err := io.ReadAll(resp.Body)
			if err != nil {
				fmt.Printf("读取响应体失败: %v\n", err)
				return
			}

			info := struct {
				Msg string `json:"msg"`
			}{}
			if err := json.Unmarshal(responseBody, &info); err != nil {
				fmt.Printf("err: %v\n", err)
			}

			fmt.Printf(info.Msg)
		},
	}

	paramCmd := &cobra.Command{
		Use:   "param <设备.分组.参数> <rt>",
		Short: "查询设备参数值",
		Args:  cobra.ExactArgs(2), // 修改：只需一个参数（设备ID）
		Run: func(cmd *cobra.Command, args []string) {
			url := fmt.Sprintf("http://%s/device/argshow/%s/%s", ConsoleAddr(), args[0], args[1])
			req, err := http.NewRequest("GET", url, http.NoBody)
			if err != nil {
				fmt.Printf("创建请求失败: %w", err)
				return
			}

			req.Header.Set("Content-Type", "application/json")

			client := &http.Client{}
			resp, err := client.Do(req)
			if err != nil {
				fmt.Printf("请求发送失败: %w", err)
				return
			}
			defer resp.Body.Close()

			// 检查响应状态码
			if resp.StatusCode != http.StatusOK {
				fmt.Printf("服务器返回错误状态码: %d", resp.StatusCode)
				return
			}

			responseBody, err := io.ReadAll(resp.Body)
			if err != nil {
				fmt.Printf("读取响应体失败: %v\n", err)
				return
			}

			info := struct {
				Msg string `json:"msg"`
			}{}
			if err := json.Unmarshal(responseBody, &info); err != nil {
				fmt.Printf("err: %v\n", err)
			}

			fmt.Printf(info.Msg)
		},
	}

	groupCmd := &cobra.Command{
		Use:   "group <设备.分组> <rt>",
		Short: "查询某个分组下的所有参数",
		Args:  cobra.ExactArgs(2), // 修改：只需一个参数（设备ID）
		Run: func(cmd *cobra.Command, args []string) {
			url := fmt.Sprintf("http://%s/device/groupshow/%s/%s", ConsoleAddr(), args[0], args[1])
			req, err := http.NewRequest("GET", url, http.NoBody)
			if err != nil {
				fmt.Printf("创建请求失败: %w", err)
				return
			}

			req.Header.Set("Content-Type", "application/json")

			client := &http.Client{}
			resp, err := client.Do(req)
			if err != nil {
				fmt.Printf("请求发送失败: %w", err)
				return
			}
			defer resp.Body.Close()

			// 检查响应状态码
			if resp.StatusCode != http.StatusOK {
				fmt.Printf("服务器返回错误状态码: %d", resp.StatusCode)
				return
			}

			responseBody, err := io.ReadAll(resp.Body)
			if err != nil {
				fmt.Printf("读取响应体失败: %v\n", err)
				return
			}

			info := struct {
				Msg string `json:"msg"`
			}{}
			if err := json.Unmarshal(responseBody, &info); err != nil {
				fmt.Printf("err: %v\n", err)
			}

			fmt.Printf(info.Msg)
		},
	}

	// 添加命令到命令组
	queryCmd.AddCommand(deviceDataCmd, paramCmd, groupCmd)
	RootCmd.AddCommand(queryCmd)
}

func getDeviceParam(deviceID, paramPath string) (string, error) {
	return fmt.Sprintf("设备 %s 参数 %s = 42", deviceID, paramPath), nil
}
